package com.hyjx.framework.uc.depart;

import com.alibaba.fastjson.JSONArray;
import com.hyjx.framework.common.ConfigParentTree;
import com.hyjx.framework.common.Constants;
import com.hyjx.framework.common.tree.TreeObject;
import com.hyjx.framework.common.tree.TreeUtil;
import com.hyjx.framework.common.util.SpringBeanUtil;
import com.hyjx.framework.common.util.StringHelper;
import com.hyjx.framework.component.codetable.CodeBean;
import com.hyjx.framework.component.codetable.CodeTableInfo;
import com.hyjx.framework.component.codetable.CodeTableManager;
import com.hyjx.framework.service.CacheService;
import com.hyjx.framework.service.ConfigManager;
import com.hyjx.framework.service.RightManager;
import com.hyjx.framework.service.SessionManager;
import com.hyjx.framework.service.UserCenterService;
import com.hyjx.framework.service.extend.IDepartExtend;
import com.hyjx.framework.uc.user.UserInfo;
import com.hyjx.framework.uc.user.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单位/部门管理 控制器
 *
 * @author Peter
 */
@Controller
@RequestMapping("/depart")
public class DepartController {
    private DepartModel model = new DepartModel();
    @Resource
    private DepartService service;
    @Resource
    private UserService userservice;

    /**
     * 跳转到列表页
     *
     * @return
     */
    @RequestMapping(value = "/gotoList", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoList() {
        return "framework/uc/depart/list";
    }

    @RequestMapping(value = "/gotoTree", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoTree() {
        return "framework/uc/depart/tree";
    }

    @RequestMapping(value = "/gotoTreeShow", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoTreeShow() {
        return "framework/uc/user/tree";
    }

    /**
     * 跳转到增加页
     *
     * @return
     */
    @RequestMapping(value = "/gotoAdd", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView gotoAdd(HttpServletRequest request) {
        String parentCode = request.getParameter("parentCode");
        String parentId = request.getParameter("parentId");
        DepartModel model = new DepartModel();
        model.setDepart_parent_code(parentId);
        model.setDepart_type("0");
        model.setValidity("Y");
        String tempcode = service.getMaxCodeByParentId(model);
        model.setDepart_code(tempcode);
        model.setOrder_no(service.getMaxOrderNoByParentId(model));
        ModelAndView mav = new ModelAndView("framework/uc/depart/add");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 跳转到修改页
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoEdit", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ModelAndView gotoEdit(HttpServletRequest request) {
        String id = request.getParameter("id");
        model = service.getDepartById(id);
        //model.setDepart_level(CodeTableManager.getNameByCode("bmcj", model.getDepart_level()));
        ModelAndView mav = new ModelAndView("framework/uc/depart/edit");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 自定义树
     * request
     *
     * @return
     */
    @RequestMapping(value = "/docustomtree", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String doCustomTree(HttpServletRequest request) {
        String type = request.getParameter("type");
        String para = request.getParameter("paras");
        if ("undefined".equals(para))
            para = "";
        String treeid = request.getParameter("treeid");
        String codetablepara = request.getParameter("codetablepara");
        if ("undefined".equals(codetablepara))
            codetablepara = "";
        //	String sql = Property.getValue(treeid);
        //自定义函数的根节点代码为#
        String rootCode = "#";
        if (type == null) {
            return null;
        } else if (type.equals("1")) {
            String sql = ConfigManager.getConfig(treeid);
            // sql 不需要传入参数的
            String finsql = sql;
            List list = service.queryObjectList(finsql);
            List<TreeObject> listfin = new ArrayList<TreeObject>();
            List<CodeBean> tmpList = list;
            for (CodeBean cb : tmpList) {
                TreeObject to = new TreeObject();
                to.setParent_id(cb.getParentCode());
                to.setText(cb.getName());
                to.setId(cb.getCode());
//				if("#".equals(cb.getParentCode())){
//					rootCode=cb.getCode();
//				}else{
//
//				}
                if ("#".equals(rootCode) && StringUtils.isEmpty(cb.getParentCode()))
                    rootCode = "";
                listfin.add(to);
            }
            String str = TreeUtil.getEasyuiJsonStr(listfin, rootCode, "");
            return str;
            /*tolist = ConfigParentTree.getTreeData(treeid, null, true);
			return TreeUtil.getEasyuiJsonStr(tolist, rootCode, "");*/
        } else if (type.equals("2") && para != null) {
            String sql = (String) CacheService.get(treeid);
            // sql 需要传入参数的
            para = para.trim();
            String finsql = sql;
            int a = para.indexOf("-");
            if (a == -1) {
                int firstindex = finsql.indexOf("???");
                finsql = finsql.substring(0, firstindex) + para + finsql.substring(firstindex + 3, finsql.length());
                List list = service.queryObjectList(finsql);
                List<TreeObject> listfin = new ArrayList<TreeObject>();
                List<CodeBean> tmpList = list;
                for (CodeBean cb : tmpList) {
                    TreeObject to = new TreeObject();
                    to.setParent_id(cb.getParentCode());
                    to.setText(cb.getName());
                    to.setId(cb.getCode());
//					if("#".equals(cb.getParentCode())){
//						rootCode=cb.getCode();
//					}
                    listfin.add(to);
                }
                return TreeUtil.getEasyuiJsonStr(listfin, rootCode, "");
            } else {
                String[] paras = para.split("-");
                for (int i = 0; i < paras.length; i++) {
                    String temp = paras[i];
                    int firstindex = finsql.indexOf("???");
                    finsql = finsql.substring(0, firstindex) + temp + finsql.substring(firstindex + 3, finsql.length());
                }
                List list = service.queryObjectList(finsql);
                List<TreeObject> listfin = new ArrayList<TreeObject>();
                List<CodeBean> tmpList = list;
                for (CodeBean cb : tmpList) {
                    TreeObject to = new TreeObject();
                    to.setParent_id(cb.getParentCode());
                    to.setText(cb.getName());
                    to.setId(cb.getCode());
//					if("#".equals(cb.getParentCode())){
//						rootCode=cb.getCode();
//					}
                    listfin.add(to);
                }
                return TreeUtil.getEasyuiJsonStr(listfin, rootCode, "");
            }

        } else if (type.equals("3")) {
            String sql = (String) CacheService.get(treeid);
            // json
            return sql;
        } else if (type.equals("4")) {
            // 多级码表
            codetablepara = codetablepara.trim();
            String[] codetableparas = codetablepara.split("-");
            List<CodeBean> codeListfin = new ArrayList<CodeBean>();
            for (int i = 0; i < codetableparas.length; i++) {
                String temp = codetableparas[i];
                CodeTableInfo info = CodeTableManager.getCodeTableMap().get(temp);
                List<CodeBean> codeList = info.getCodeList();
                for (int j = 0; j < codeList.size(); j++) {
                    codeListfin.add(codeList.get(j));
                }
            }
            //自定义一个根节点
//			TreeObject root=new TreeObject();
//			root.setId("-1");
//			root.setText("自定义根");
            List<TreeObject> listfin = new ArrayList<TreeObject>();
            for (CodeBean cb : codeListfin) {
                TreeObject to = new TreeObject();
//				if("#".equals(cb.getParentCode())){
//					cb.setParentCode(root.getId());
//				}
                if ("#".equals(rootCode) && StringUtils.isEmpty(cb.getParentCode()))
                    rootCode = "-1";
                to.setParent_id(cb.getParentCode());
                to.setText(cb.getName());
                to.setId(cb.getCode());
                to.setCode(cb.getCode());
                listfin.add(to);
            }
            String str = TreeUtil.getEasyuiJsonStr(listfin, rootCode, "");
            return str;

        } else {
            return null;
        }
    }

    /**
     * 生成树视图数据 单位树
     *
     * @param request
     * @return
     */
    @SuppressWarnings({"unchecked"})
    @RequestMapping(value = "/doTree", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String doTree(HttpServletRequest request) {
        String bmid = request.getParameter("rootid");
        if ("null".equals(bmid))
            bmid = "-1";
        if (bmid == null || bmid.trim().length() == 0) {
            //List<RoleModel> rolelist= (List<RoleModel>) UserCenterService.getCurrentUserInfo().getRolelist();
            UserInfo user = (UserInfo) SessionManager.get(Constants.USERINFO);
            if (!user.isSuperAdmin()) {
                //直接使用此字段，已计算好
                bmid = UserCenterService.getCurrentUserInfo().getDataOrgCode();
            }
        }
        String checked = request.getParameter("checked");
        String usertree = request.getParameter("usertree");
        String departtype = request.getParameter("departtype");
        String pid = request.getParameter("pid");
        String[] checks = null;
        if (checked != null && checked.trim().length() > 0) {
            checks = checked.split(",");
        }
        String treeid = request.getParameter("treeid");
        List<TreeObject> tolist = null;
        DepartModel departModel = new DepartModel();
        departModel.setDepart_code(bmid);
        String rootCode = "";
        //自定义数根节点要从新取
        if (treeid != null && treeid.trim().length() > 0) {
            //自定义树
            String type = request.getParameter("type");
            String para = request.getParameter("para");
            String sql = ConfigManager.getConfig(treeid);
            if (type != null) {
                if (type.equals("1")) {
                    // sql 不需要传入参数的
                    String finsql = sql;
                    List list = service.queryObjectList(finsql);
                    List<TreeObject> listfin = new ArrayList<TreeObject>();
                    List<CodeBean> tmpList = list;
                    for (CodeBean cb : tmpList) {
                        TreeObject to = new TreeObject();
                        to.setParent_id(cb.getParentCode());
                        to.setText(cb.getName());
                        to.setId(cb.getCode());
                        listfin.add(to);
                    }
                    return TreeUtil.getEasyuiJsonStr(listfin, null, "");
					/*tolist = ConfigParentTree.getTreeData(treeid, null, true);
					return TreeUtil.getEasyuiJsonStr(tolist, rootCode, "");*/
                } else if (type.equals("2") && para != null) {
                    // sql 需要传入参数的
                    para = para.trim();
                    String finsql = sql;
                    String[] paras = para.split(",");
                    for (int i = 0; i < paras.length; i++) {
                        String temp = paras[i];
                        int firstindex = finsql.indexOf("???");
                        finsql = finsql.substring(0, firstindex) + temp + finsql.substring(firstindex + 3, finsql.length());
                    }
                    List list = service.queryObjectList(finsql);
                    List<TreeObject> listfin = new ArrayList<TreeObject>();
                    List<CodeBean> tmpList = list;
                    for (CodeBean cb : tmpList) {
                        TreeObject to = new TreeObject();
                        to.setParent_id(cb.getParentCode());
                        to.setText(cb.getName());
                        to.setId(cb.getCode());
                        listfin.add(to);
                    }
                    return TreeUtil.getEasyuiJsonStr(listfin, null, "");
                } else if (type.equals("3")) {
                    // json
                    return sql;
                } else if (type.equals("4")) {
                    // 多级码表
                    String codetablepara = request.getParameter("codetablepara");
                    codetablepara = codetablepara.trim();
                    String[] codetableparas = codetablepara.split("-");
                    List<CodeBean> codeListfin = new ArrayList<CodeBean>();
                    for (int i = 0; i < codetableparas.length; i++) {
                        String temp = codetableparas[i];
                        CodeTableInfo info = CodeTableManager.getCodeTableMap().get(temp);
                        List<CodeBean> codeList = info.getCodeList();
                        for (int j = 0; j < codeList.size(); j++) {
                            codeListfin.add(codeList.get(j));
                        }
                    }
                    List<TreeObject> listfin = new ArrayList<TreeObject>();
                    for (CodeBean cb : codeListfin) {
                        TreeObject to = new TreeObject();
                        to.setParent_id(cb.getParentCode());
                        to.setText(cb.getName());
                        to.setId(cb.getCode());
                        listfin.add(to);
                    }
                    return TreeUtil.getEasyuiJsonStr(listfin, null, "");

                }
            } else {
                tolist = ConfigParentTree.getTreeData(treeid, null, true);
                return TreeUtil.getEasyuiJsonStr(tolist, null, "");
            }
        }
        List<DepartModel> list = new ArrayList<DepartModel>();
        if (departtype != null && departtype.trim().equals("1")) {
            DepartModel dm1 = new DepartModel();
            if (StringUtils.isNoneEmpty(bmid))
                if (!"-1".equals(pid))
                    dm1.setDepart_parent_code(pid);
                else {
                    if (!"-1".equals(bmid)) {
                        dm1.setRootid(bmid);
                    }
                }


            list = service.getUnit(dm1);
        } else if (bmid != null && bmid.trim().length() > 0 && departtype != null && departtype.trim().equals("2")) {
            if (bmid.indexOf(",") == -1) {
                DepartModel dm1 = RightManager.getDepartByCode(bmid);
                List<DepartModel> list1 = null;
                if (pid != null && pid.trim().length() > 0) {
                    String validity = request.getParameter("validity");
                    if (!StringUtils.isEmpty(validity) && "-1".equals(validity))
                        validity = "";
                    else validity = "Y";
                    departModel.setValidity(validity);
                    
                    departModel.setDepart_parent_code("-1".equalsIgnoreCase(pid) ? pid : bmid);
                    //departModel.setDepart_parent_code(pid);
                    list1 = service.getDepartModelList(departModel);
                } else
                    list1 = service.getAllSubDepart(dm1);
                list.addAll(list1);
            } else
                list = service.getSomeDepart(bmid);
        } else {
            list = service.getAllDepart(new DepartModel());
        }
        if ("1".equals(usertree)) {
            List<DepartModel> userlist = null;
            try {
                userlist = userservice.getUser4Tree();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            list.addAll(userlist);
        }
        List<TreeObject> treeList = new ArrayList<TreeObject>();
        String checkedfull = "";
        for (DepartModel dm : list) {
            if (dm.getDepart_code().equals(checked))
                checkedfull = dm.getDepart_code_full();
            TreeObject to = new TreeObject();
            to.setAttributes(dm.getOrg_code());
            to.setCode(dm.getDepart_code());
            to.setId(dm.getDepart_id());
            if (usertree == null) {
                to.setParent_id(dm.getDepart_parent_code());
            } else {
                to.setParent_id(dm.getDepart_parent_code());
            }

            to.setChecked(setChecked(to.getId(), checks));
            to.setSelected(to.isChecked());
            to.setText(dm.getDepart_name());
            to.setUrl("");

            if ("1".equals(dm.getIs_leaf_node()))
                to.setState("open");
            else
                to.setState("closed");
            treeList.add(to);
        }
//		if(StringUtils.isNoneEmpty(checkedfull))
//		{
//			String[] checkedfullarray =checkedfull.split("\\.");
//			for(TreeObject to : treeList)
//			{
//				for(String s : checkedfullarray)
//				{
//					if(to.getId().equals(s))
//						to.setState("open");
//				}
//			}
//		}
        String jsonString = null;
        List<TreeObject> jsonList = tolist != null ? tolist : treeList;
        String rootid = bmid == null || bmid.trim().length() == 0 ? rootCode : bmid;
        //System.out.println(JSONArray.toJSONString(jsonList));
        return JSONArray.toJSONString(jsonList);
        //jsonString=TreeUtil.getEasyuiJsonStr(jsonList, rootid,"");
        //return jsonString;
    }

    @SuppressWarnings("unused")
    private List<TreeObject> getTreeObjectChild(TreeObject to, List<DepartModel> list) {
        List<TreeObject> tolist = new ArrayList<TreeObject>();
        Map<String, TreeObject> tomap = new HashMap<String, TreeObject>();
        for (DepartModel dm : list) {
            if (dm.getDepart_parent_code() != null && dm.getDepart_parent_code().equals(to.getCode())) {
                if (!tomap.containsKey(dm.getDepart_code())) {
                    TreeObject to2 = new TreeObject();
                    to2.setCode(dm.getDepart_code());
                    //to2.setId(dm.getDepart_id());
                    to2.setParent_id(dm.getDepart_parent_code());
                    to2.setText(dm.getDepart_name());
                    tomap.put(dm.getDepart_code(), to2);
                }
            }
        }
        if (tomap.size() > 0)
            tolist.addAll(tomap.values());
        return tolist;
    }

    private boolean setChecked(String val, String[] checks) {
        if (val == null) return false;
        if (checks == null || checks.length == 0)
            return false;
        else {
            for (String s : checks) {
                if (s.toLowerCase().equals(val.toLowerCase())) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 修改信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    @ResponseBody
    public String doAdd(DepartModel model) {
        model.setDepart_id(StringHelper.getPrimaryKey());
        model.setDepart_code_full(service.getDepartByCode(model.getDepart_parent_code()).getDepart_code_full() + model.getDepart_code() + ".");
        model.setDepart_name(model.getDepart_name().trim());
        model.setIs_leaf_node("1");
        model.setOrg_code(model.getDepart_parent_code());
        if ("1".equals(model.getDepart_type())) {
            model.setOrg_code(model.getDepart_code());
        } else {
            model.setOrg_code(service.getDepartByCode(model.getDepart_parent_code()).getDepart_code());
        }

        IDepartExtend departExtend = (IDepartExtend) SpringBeanUtil.getBean("departExtendService");
        departExtend.beforeAdd(model);
        model.setCreate_time(new Date());
        if (service.add(model)) {
            departExtend.afterAdd(model);
            service.add2(model);
            RightManager.init(1);
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 修改信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/doEdit", method = RequestMethod.POST)
    @ResponseBody
    public String doEdit(DepartModel model) {
        IDepartExtend departExtend = (IDepartExtend) SpringBeanUtil.getBean("departExtendService");
        departExtend.beforeUpdate(model);
        model.setUpdate_time(new Date());
        if (service.update(model) > 0) {
            service.update2(model);
            departExtend.afterUpdate(model);
            RightManager.init(1);
            return "success";
        } else {
            return "failure";
        }
    }

    @RequestMapping(value = "/doDelete")
    @ResponseBody
    public String doDelete(String id) {
        IDepartExtend departExtend = (IDepartExtend) SpringBeanUtil.getBean("departExtendService");
        departExtend.beforeDelete(id);
        if (service.logic_delete(id) > 0) {
            departExtend.afterDelete(id);
            service.delete2(id);
            userservice.disableUserByDeptCode(id);
            RightManager.init(1);
            return "success";
        } else {
            return "failure";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/validate", produces = "application/text; charset=utf-8")
    public String validate(DepartModel model) {
        return service.validate(model);
    }

    @ResponseBody
    @RequestMapping(value = "/getDeptName", produces = "application/json; charset=utf-8")
    public String getDeptName(String departCode) {
        DepartModel model = service.getDepartByCode(departCode);
        return model.getDepart_name();
    }

    @RequestMapping(value = "/gotoView", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        DepartModel model = service.getDepartByCode(id);
        model.setCreate_time(model.getCreate_time());
        ModelAndView mav = new ModelAndView("framework/uc/depart/view");
        mav.addObject("model", model);
        return mav;
    }

    public DepartModel getModel() {
        return model;
    }

    public void setModel(DepartModel model) {
        this.model = model;
    }

    /**
     * 返回easyui tree结构的Json字符串
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/departtree", produces = "application/json; charset=utf-8")
    public String easyuiusertree(HttpServletRequest request) {
        String orgCode = request.getParameter("rootid");
        String checked = request.getParameter("checked");
        if (checked == null) {
            checked = "";
        }
        if (orgCode == null || "".equals(orgCode)) {
            orgCode = UserCenterService.getCurrentUserInfo().getOrg_code();
        }
        String departType = request.getParameter("departtype");
        if (departType == null || "".equals(departType)) {
            departType = "2";
        }

        List<DepartModel> list = RightManager.getSubDepartsByOrgCode(departType, orgCode);
        List<TreeObject> dlist = new ArrayList<TreeObject>();
        for (DepartModel model : list) {
            TreeObject tree = new TreeObject();
            if ("#".equals(model.getDepart_parent_code())) {
                orgCode = model.getDepart_id();
            }
            tree.setParent_id(model.getDepart_parent_code());
            tree.setId(model.getDepart_id());
            tree.setCode(model.getDepart_code());
            tree.setText(model.getDepart_name());
            dlist.add(tree);
        }
        String result = TreeUtil.getEasyuiJsonStr(dlist, orgCode, checked);
        return result;
    }

    //测试自定义树
    @RequestMapping(value = "/customtree", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String CustomTree(HttpServletRequest request) {
        String type = request.getParameter("type");
        String para = request.getParameter("paras");
        if ("undefined".equals(para))
            para = "";
        String treeid = request.getParameter("treeid");
        String codetablepara = request.getParameter("codetablepara");
        if ("undefined".equals(codetablepara))
            codetablepara = "";
        //自定义函数的根节点代码为#
        String rootCode = "#";
        if (type == null) {
            return null;
        } else if (type.equals("1")) {
            String finsql = ConfigManager.getConfig(treeid);
            // sql 不需要传入参数的
            List<CodeBean> tmpList = service.queryObjectList(finsql);
            List<TreeObject> listfin = new ArrayList<TreeObject>();
            for (CodeBean cb : tmpList) {
                TreeObject to = new TreeObject();
                to.setParent_id(cb.getParentCode());
                to.setText(cb.getName());
                to.setId(cb.getCode());
                to.setCode(cb.getCode());
                if ("#".equals(rootCode) && StringUtils.isEmpty(cb.getParentCode()))
                    rootCode = "";
                listfin.add(to);
            }
            String str = TreeUtil.getEasyuiJsonStr(listfin, rootCode, "");
            return str;
        } else if (type.equals("2") && para != null) {
            String finsql = (String) CacheService.get(treeid);
            int firstindex = 0;
            int secondindex = 0;
            int thirdindex = 0;
            // sql 需要传入参数的
            para = para.trim();

            int a = para.indexOf("-");
            if (a == -1) {
                firstindex = finsql.indexOf("???");
                finsql = finsql.substring(0, firstindex) + para + finsql.substring(firstindex + 3, finsql.length());
            } else {
                String[] paras = para.split("-");
                String tempValue = "";
                firstindex = finsql.indexOf("???");
                secondindex = finsql.indexOf("where");
                thirdindex = finsql.indexOf("=");
                String condition = finsql.substring(secondindex + 6, thirdindex);
                for (int i = 0; i < paras.length; i++) {
                    String temp = paras[i];
                    if (i == 0) {
                        tempValue = "'" + temp + "'";
                    } else {
                        tempValue += " or " + condition + " = " + "'" + temp + "'";
                    }
                }
                finsql = finsql.substring(0, firstindex - 1) + tempValue + finsql.substring(firstindex + 4, finsql.length());
            }
            List list = service.queryObjectList(finsql);
            List<TreeObject> listfin = new ArrayList<TreeObject>();
            List<CodeBean> tmpList = list;
            for (CodeBean cb : tmpList) {
                TreeObject to = new TreeObject();
                to.setParent_id(cb.getParentCode());
                to.setText(cb.getName());
                to.setId(cb.getCode());
                listfin.add(to);
            }
            return TreeUtil.getEasyuiJsonStr(listfin, rootCode, "");

        } else if (type.equals("3")) {
            String sql = ConfigManager.getConfig(treeid);
            // json
            return sql;
        } else if (type.equals("4")) {
            // 多级码表
            codetablepara = codetablepara.trim();
            String[] codetableparas = codetablepara.split("-");
            List<CodeBean> codeListfin = new ArrayList<CodeBean>();
            for (int i = 0; i < codetableparas.length; i++) {
                String temp = codetableparas[i];
                CodeTableInfo info = CodeTableManager.getCodeTableMap().get(temp);
                List<CodeBean> codeList = info.getCodeList();
                for (int j = 0; j < codeList.size(); j++) {
                    codeListfin.add(codeList.get(j));
                }
            }
            List<TreeObject> listfin = new ArrayList<TreeObject>();
            for (CodeBean cb : codeListfin) {
                TreeObject to = new TreeObject();
                to.setParent_id(cb.getParentCode());
                to.setText(cb.getName());
                to.setId(cb.getCode());
                to.setCode(cb.getCode());
                listfin.add(to);
            }
            String str = TreeUtil.getEasyuiJsonStr(listfin, rootCode, "");
            return str;

        } else {
            return null;
        }
    }
}